//
//  ViewController.m
//  LanguageManager
//
//  Created by YQ on 2021/9/3.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic, strong) UIButton *btOne;
@property (nonatomic, strong) UIButton *btTwo;
@property (nonatomic, strong) UIButton *btThree;

@property (nonatomic, strong) UILabel *lbOne;
@property (nonatomic, strong) UILabel *lbTwo;
@property (nonatomic, strong) UILabel *lbThree;

@property (nonatomic, strong) UIImageView *imageOne;
@property (nonatomic, strong) UIImageView *imageTwo;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self btOne];
    [self btTwo];
    [self btThree];
    [self lbOne];
    [self lbTwo];
    [self lbThree];
    [self imageOne];
    [self imageTwo];
}

- (void)clickBTone:(UIButton *)ser{
    NSLog(@"中文21");
}

- (void)clickBTtwo:(UIButton *)ser{
    NSLog(@"英文");
}

- (void)clickBTthree:(UIButton *)ser{
    NSLog(@"权限");
    
    [self getPermission];
}

- (void)getPermission {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (authStatus == AVAuthorizationStatusNotDetermined) {
        NSLog(@"没有询问是否开启麦克风");
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     if (granted) {
                         NSLog(@"授权成功");
                     } else {
                         NSLog(@"第一次未授权，用户限制");
                     }
                 });
            }];
        } else if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
            NSLog(@"未授权，用户限制");
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您没有开启\"麦克风\"权限\n 无法进行语音识别。请在设置中开启麦克风权限。" preferredStyle:(UIAlertControllerStyleAlert)];
             UIAlertAction *actionDefault = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                 NSLog(@"取消");
             }];
            UIAlertAction *actionCance = [UIAlertAction actionWithTitle:@"去开启" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"去设置开启权限");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                });
            }];
            [alert addAction:actionDefault];
            [alert addAction:actionCance];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        } else if (authStatus == AVAuthorizationStatusAuthorized) {
            NSLog(@"授权成功");
        }
}


#pragma mark - lazy

- (UIButton *)btOne{
    if (!_btOne) {
        _btOne = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_btOne setTitle:NSLocalizedString(@"Chinese", nil) forState:(UIControlStateNormal)];
        [_btOne setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _btOne.titleLabel.font = [UIFont systemFontOfSize:20];
        _btOne.frame = CGRectMake(50, 50, 100, 50);
        [_btOne addTarget:self action:@selector(clickBTone:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_btOne];
    }
    return _btOne;
}

- (UIButton *)btTwo{
    if (!_btTwo) {
        _btTwo = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_btTwo setTitle:NSLocalizedString(@"English", nil) forState:(UIControlStateNormal)];
        [_btTwo setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _btTwo.titleLabel.font = [UIFont systemFontOfSize:20];
        _btTwo.frame = CGRectMake(150, 50, 100, 50);
        [_btTwo addTarget:self action:@selector(clickBTtwo:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_btTwo];
    }
    return _btTwo;
}

- (UIButton *)btThree{
    if (!_btThree) {
        _btThree = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_btThree setTitle:NSLocalizedString(@"Authority", nil) forState:(UIControlStateNormal)];
        [_btThree setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _btThree.titleLabel.font = [UIFont systemFontOfSize:20];
        _btThree.frame = CGRectMake(250, 50, 100, 50);
        [_btThree addTarget:self action:@selector(clickBTthree:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_btThree];
    }
    return _btThree;
}

- (UILabel *)lbOne{
    if (!_lbOne) {
        _lbOne = [[UILabel alloc] init];
        _lbOne.frame = CGRectMake(50, 200, 100, 50);
        _lbOne.text = NSLocalizedString(@"User", nil);
        _lbOne.font = [UIFont systemFontOfSize:20];
        [self.view addSubview:_lbOne];
    }
    return _lbOne;
}

- (UILabel *)lbTwo{
    if (!_lbTwo) {
        _lbTwo = [[UILabel alloc] init];
        _lbTwo.frame = CGRectMake(150, 200, 100, 50);
        _lbTwo.text = NSLocalizedString(@"Password", nil);
        _lbTwo.font = [UIFont systemFontOfSize:20];
        [self.view addSubview:_lbTwo];
    }
    return _lbTwo;
}

// 数字国际化
- (UILabel *)lbThree{
    if (!_lbThree) {
        _lbThree = [[UILabel alloc] init];
        _lbThree.frame = CGRectMake(250, 200, 100, 50);
        _lbThree.font = [UIFont systemFontOfSize:20];
        
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
        NSString *numberString = [numberFormatter stringFromNumber:@(-1000000)];
        _lbThree.text = numberString;
        
        [self.view addSubview:_lbThree];
    }
    return _lbThree;
}

// 图片获取 方法一
- (UIImageView *)imageOne{
    if (!_imageOne) {
        _imageOne = [[UIImageView alloc] initWithFrame:CGRectMake(50, 250, 100, 100)];
        _imageOne.image = [UIImage imageNamed:NSLocalizedString(@"imageName", nil)];
        [self.view addSubview:_imageOne];
    }
    return _imageOne;
}

// 图片获取 方法二
- (UIImageView *)imageTwo{
    if (!_imageTwo) {
        _imageTwo = [[UIImageView alloc] initWithFrame:CGRectMake(250, 250, 100, 100)];
        _imageTwo.image = [UIImage imageNamed:@"image"];
        [self.view addSubview:_imageTwo];
    }
    return _imageTwo;
}
@end
